package com.example.userclient.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.example.userclient.AddBookExtension
import com.example.userclient.R
import com.example.userclient.databinding.BuyBookItemBinding
import java.sql.Timestamp

object BuyBookListAdapter :
    RecyclerView.Adapter<BuyBookListAdapter.ViewHolder>() {

    var dataSet: Array<AddBookExtension>? = null

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.buy_book_item, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        dataSet?.get(position)?.let {

            holder.changeData(it)
        }
    }



    override fun getItemCount()= dataSet!!.size



    class ViewHolder(private val view: View) : RecyclerView.ViewHolder(view) {

        private val mBinding: BuyBookItemBinding? = DataBindingUtil.bind(view)

        fun changeData(addBookExtension: AddBookExtension) {
            if (mBinding != null) {
                addBookExtension.buyTime = AddBookListAdapter.format.format(Timestamp(addBookExtension.sellTime))
                mBinding.book = addBookExtension

                Log.i("BuyBookListAdapter", "changeData")
            }
        }
    }
}